import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.event.*;
import java.util.*;
import com.toedter.calendar.*;
import java.sql.*;
import java.text.SimpleDateFormat;


public class stockGui extends JPanel implements ActionListener
{
	
	private JPanel baseP=null, northP=null, northP2=null, northP3=null, eastP=null, 
					westP=null, southP=null, baseP2=null, eastP2=null, westP2=null,
					eastP3=null, westP3=null, centerP = null;
	private JLabel searchLbl = null, stockAttrib = null;
	private JTextField searchTxt = null;
	private JButton searchB = null, editB = null, delB= null, searchStockB = null, addStockB = null, returnB = null, addB=null;
	private JComboBox searchJcb = null, genreJcb=null, statusJcb=null, mediaTypeJcb=null, catNameJcb=null;
	private Vector searchV = null, genreV=null, statusV=null, mediaTypeV=null, catNameV;
	private JScrollPane jsp = null, scrollP=null, genreJsp=null, statusJsp=null, mediaTypeJsp=null, catNameJsp=null;
	private JTextArea jta = null;
	private JLabel mediaStockId=null, mediaTypeName=null, releaseDate=null,genre=null
					,ageRest=null, title=null, serialNo=null, status=null , director=null
					, barcode=null, brand=null, description=null, catId=null, runTime=null
					, actor1=null, actor2=null, dateLastRented=null;
	private JTextField medStockIdTxt = null, directorTxt=null
					, ageRestTxt=null, titleTxt=null, serialNoTxt=null
					, barcodeTxt=null, brandTxt=null, descriptionTxt=null, catIdTxt=null, runTimeTxt=null
					, actor1Txt=null, actor2Txt = null;
	private JDateChooser lastRentedJdc = null, releaseDateJdc=null; 								
	private String myDriver = "com.mysql.jdbc.Driver";
	private	String ipOfDB = "localhost";
	private	MySQLdriverConnection msc;
	private	Connection Conn;
	private SimpleDateFormat sdf=null;
	addStock AddStock = null;
	searchGui SearchGui = null;
	
	public stockGui() 
	{
		baseP = new JPanel(new BorderLayout());
		
		northP = new JPanel(new GridLayout(1,2));
		searchStockB = new JButton("Search-4-Stock Item");
		searchStockB.setToolTipText("Find Stock Item");
		searchStockB.addActionListener(this);
		addStockB = new JButton("Add Stock");
		addStockB.setToolTipText("Add New Stock Item");
		addStockB.addActionListener(this);
		northP.add(searchStockB);
		northP.add(addStockB);
		
		baseP2 = new JPanel(new BorderLayout());
		
		baseP.add(northP, BorderLayout.NORTH);
		baseP.add(baseP2, BorderLayout.CENTER);
		
		northP2 = new JPanel( );
		
		BoxLayout b1 = new BoxLayout(northP2, BoxLayout.X_AXIS);
		northP2.setLayout(b1);
		searchLbl = new JLabel(" Enter Stock Item Name ");
		searchTxt = new JTextField();
		searchV = new Vector();
		searchV.addElement(" <Select> ");
		searchV.addElement(" Name ");
		searchV.addElement(" Stock ID ");
		searchV.addElement(" Serial No. ");
		searchJcb = new JComboBox(searchV);
		scrollP = new JScrollPane(searchJcb ,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		searchB = new JButton(" Search ");
		northP2.add(searchLbl);
		northP2.add(searchTxt);
		northP2.add(searchJcb);
		northP2.add(searchB);
		northP2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		westP = new JPanel(new BorderLayout());
		
		northP3 = new JPanel();
		stockAttrib = new JLabel(" Media Stock Attributes ");
		northP3.add(stockAttrib);
		
		centerP = new JPanel();
		
		
		westP.add(centerP, BorderLayout.CENTER);
		westP.add(northP3, BorderLayout.NORTH);
		
		eastP = new JPanel();
		BoxLayout b3 = new BoxLayout(eastP, BoxLayout.Y_AXIS);
		eastP.setLayout(b3);
		jta = new JTextArea(null, 15, 28);
		jsp = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
							JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
							
		eastP.add(jsp);					
		eastP3 = new JPanel();
		
		medStockIdTxt = new JTextField(25);
		barcodeTxt = new JTextField(25);
		medStockIdTxt.setEditable(false);
		barcodeTxt = new JTextField(25);
		brandTxt = new JTextField(25);
		serialNoTxt = new JTextField(25);
		descriptionTxt = new JTextField(25);
		catIdTxt = new JTextField(25);
		titleTxt = new JTextField(25);
		runTimeTxt = new JTextField(25);
		ageRestTxt = new JTextField(25);		
		directorTxt = new JTextField(25);
		actor1Txt = new JTextField(25);
		actor2Txt = new JTextField(25);
		
		lastRentedJdc = new JDateChooser();
		releaseDateJdc = new JDateChooser();
				
		mediaStockId = new JLabel(" Stock ID ");
		barcode = new JLabel(" Barcode ");
		mediaTypeName = new JLabel(" Media Type Name ");
		
		brand = new JLabel(" Brand ");
		barcode = new JLabel(" Barcode ");
		serialNo = new JLabel(" Serial No. ");
		description = new JLabel(" Description ");
		catId = new JLabel(" Category ID ");
		title = new JLabel(" Title ");
		status = new JLabel(" Status ");
		releaseDate = new JLabel(" Release Date");
		genre = new JLabel(" Genre ");
		runTime = new JLabel(" Run Time ");
		ageRest = new JLabel(" Age Restriction ");
		director = new JLabel(" Director ");
		actor1 = new JLabel(" Actor 1 ");
		actor2 = new JLabel(" Actor 2 ");
		dateLastRented = new JLabel(" Date Last Rented ");
		
		genreV = new Vector();
		genreV.addElement(" <Select> ");
		genreV.addElement(" Action ");
		genreV.addElement(" Adventure ");
		genreV.addElement(" Animation ");
		genreV.addElement(" Biography ");
		genreV.addElement(" Comedy ");
		genreV.addElement(" Crime ");
		genreV.addElement(" Documentary ");
		genreV.addElement(" Drama ");
		genreV.addElement(" Family ");
		genreV.addElement(" Fantasy ");
		genreV.addElement(" Film-Noir ");
		genreV.addElement(" Game-Show ");
		genreV.addElement(" History ");
		genreV.addElement(" Horror ");
		genreV.addElement(" Music ");
		genreV.addElement(" Musical ");
		genreV.addElement(" Mystery ");
		genreV.addElement(" News ");
		genreV.addElement(" Reality-TV ");
		genreV.addElement(" Romance ");
		genreV.addElement(" Sci-Fi ");
		genreV.addElement(" Short ");
		genreV.addElement(" Sport ");
		genreV.addElement(" Talk-Show ");
		genreV.addElement(" Thriller ");
		genreV.addElement(" War ");
		genreV.addElement(" Western ");
		
		genreJcb = new JComboBox(genreV);
		genreJsp = new JScrollPane(genreJcb ,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		try
		{
			msc = new MySQLdriverConnection("","",ipOfDB,"turtledata");
			Conn = msc.getConnection();
		}
		catch(SQLException sqlE)
		{
			sqlE.printStackTrace();
		}	
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
	
		catNameV = new Vector();
		
		try
		{
			Statement popCatNameVectStmt = Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			ResultSet catNameRS;
			String priceCatTableQuery = "SELECT catName FROM pricecattable";
			catNameRS = popCatNameVectStmt.executeQuery(priceCatTableQuery);
			String tempSTR;
			catNameRS.last();
			int rows = catNameRS.getRow();
			catNameRS.first();
			catNameV = new Vector();
			for(int i = 1;i <= rows;i++ )
			{
				tempSTR = catNameRS.getString(1);
				
				System.out.println(""+tempSTR);
				catNameV.addElement(tempSTR);
				
				catNameRS.next();
			}
		}
		catch(SQLException sqlE)
		{
			System.out.println("No problemo");
			sqlE.printStackTrace();
		}//end catch
		
		catNameJcb = new JComboBox(catNameV); 
		catNameJsp = new JScrollPane(catNameJcb , JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	
		mediaTypeV = new Vector();
		mediaTypeV.addElement(" <Select> ");
		try
		{
			Statement popMediaVectStmt = Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			ResultSet mediaVectRS;
			String stockTypeTableQuery = "SELECT stockTypeName FROM stockTypeTable";
			mediaVectRS = popMediaVectStmt.executeQuery(stockTypeTableQuery);
			String tempSTR;
			mediaVectRS.last();
			int rows = mediaVectRS.getRow();
			mediaVectRS.first();
			mediaTypeV = new Vector();
			for(int i = 1;i <= rows;i++ )
			{
				tempSTR = mediaVectRS.getString(1);
				
				System.out.println(""+tempSTR);
				mediaTypeV.addElement(tempSTR);
				
				mediaVectRS.next();
			}
		}
		catch(SQLException sqlE)
		{
			System.out.println("No problemo");
			sqlE.printStackTrace();
		}//end catch
		
		mediaTypeJcb = new JComboBox(mediaTypeV); 
		mediaTypeJsp = new JScrollPane(mediaTypeJcb , JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		statusV = new Vector();
		statusV.addElement(" In ");	
		statusV.addElement(" Out ");
		statusV.addElement(" Damaged ");
		statusV.addElement(" Unavailable ");
		
		statusJcb = new JComboBox(statusV);
		statusJsp = new JScrollPane(statusJcb ,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
				
		GroupLayout layout  = new GroupLayout(eastP3);
		layout.setAutoCreateContainerGaps(true);
		layout.setAutoCreateGaps(true);
		eastP3.setLayout(layout);
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		
		vGroup.addGroup(layout.createParallelGroup().
        addComponent(mediaStockId).addComponent(mediaTypeName).
        addComponent(title).addComponent(status).
        addComponent(barcode).addComponent(brand).
        addComponent(serialNo).addComponent(description).
        addComponent(catId).addComponent(runTime).
        addComponent(releaseDate).addComponent(genre).
        addComponent(ageRest).addComponent(serialNo).
        addComponent(director).addComponent(actor1).
        addComponent(actor2).addComponent(dateLastRented));
  	    vGroup.addGroup(layout.createParallelGroup().
        addComponent(medStockIdTxt).addComponent(mediaTypeJcb).
        addComponent(titleTxt).addComponent(statusJcb).
        addComponent(barcodeTxt).addComponent(brandTxt).
        addComponent(serialNoTxt).addComponent(descriptionTxt).
        addComponent(catIdTxt).addComponent(runTimeTxt).
        addComponent(releaseDateJdc).addComponent(genreJcb).
        addComponent(ageRestTxt).addComponent(serialNoTxt).
        addComponent(directorTxt).addComponent(actor1Txt).
        addComponent(actor2Txt).addComponent(lastRentedJdc));
   		layout.setHorizontalGroup(vGroup);
   		   		   		
   		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(mediaStockId).addComponent(medStockIdTxt));
   		hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(mediaTypeName).addComponent(mediaTypeJcb));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(title).addComponent(titleTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(status).addComponent(statusJcb));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(barcode).addComponent(barcodeTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(brand).addComponent(brandTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(serialNo).addComponent(serialNoTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(description).addComponent(descriptionTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(catId).addComponent(catIdTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(runTime).addComponent(runTimeTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(releaseDate).addComponent(releaseDateJdc));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(genre).addComponent(genreJcb));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(ageRest).addComponent(ageRestTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(director).addComponent(directorTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(actor1).addComponent(actor1Txt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(actor2).addComponent(actor2Txt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(dateLastRented).addComponent(lastRentedJdc));
        layout.setVerticalGroup(hGroup);
        
		centerP.add(eastP3);
				
		southP = new JPanel();
		BoxLayout b4 = new BoxLayout(southP, BoxLayout.X_AXIS);
		southP.setLayout(b4);
		editB = new JButton(" Edit ");
		editB.setToolTipText(" Edit stock item ");
		
		delB = new JButton(" Delete ");
		delB.setToolTipText(" Delete stock item ");	
		
		returnB = new JButton(" Return "); 
		returnB.setToolTipText(" Return to main page ");
		
		southP.add(editB);
		southP.add(delB);
		southP.add(returnB);	
		southP.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		baseP2.add(northP2, BorderLayout.NORTH);
		baseP2.add(westP, BorderLayout.WEST);
		baseP2.add(eastP, BorderLayout.EAST);
		baseP2.add(southP, BorderLayout.SOUTH);
		
		AddStock = new addStock();
		SearchGui = new searchGui();
		
		//setup "this" Jpanel
		this.setLayout(new GridLayout(1,1));
		this.add(baseP);
		addStockB.doClick();
		searchStockB.doClick();
			
	}

	public void actionPerformed(ActionEvent ae)
	{
		Object source = ae.getSource();
		if(source == searchStockB)
		{
			try
			{
				baseP2.remove(westP);
				baseP2.remove(eastP);
				baseP2.remove(southP);
				baseP2.add(SearchGui);
				this.validate();
				this.repaint();
			}
			catch(Exception excep)
			{
				excep.printStackTrace();
			}	
		}
		if(source == addStockB)
		{
			try
			{
				baseP2.remove(westP);
				baseP2.remove(eastP);
				baseP2.remove(southP);
				baseP2.add(AddStock);
				this.validate();
				this.repaint();
			}
			catch(Exception excep)
			{
				excep.printStackTrace();
			}
		}
		
		if(source == addB)
		{
			java.util.Date date = releaseDateJdc.getDate();
			
			//Chris, James - This section works to get the 
			//date formatted correctly. After the format,
			//simply printing bdate.toString() renders the date as
			//yyyy-MM-dd.
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			String bdate = sdf.format(date);
			java.util.Date date2 = lastRentedJdc.getDate();
			String jdate = sdf.format(date2);
			StockManager sm = new StockManager();
			
			String [] attr = new String [15];
			attr[0] = sm.generateStockId();
			attr[1] = (String )mediaTypeJcb.getSelectedItem();
			attr[2] = titleTxt.getText();
			attr[3] = (String)statusJcb.getSelectedItem();
			attr[4] = barcodeTxt.getText();
			attr[5] = serialNoTxt.getText();
			attr[6] = descriptionTxt.getText();
			attr[7] = (String)catNameJcb.getSelectedItem();
			attr[8] = runTimeTxt.getText();
			attr[9] = bdate;
			attr[10] = (String)genreJcb.getSelectedItem();
			attr[11] = ageRestTxt.getText();
			attr[12] = directorTxt.getText();
			attr[13] = actor1Txt.getText();
			attr[14] = actor2Txt.getText();
			attr[15] = jdate;
						
			for(int i = 0; i < attr.length; i++)
			{
				System.out.println(attr[i]);
			}
			
			
			Stock stock = new Stock(attr);
			sm.commitStock(stock);
					
		}
		
		
	}
	
}
